import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class CreateNewSinglePaymentDialog {
	protected CreateNewSinglePaymentDialog( final Shell shell, final Display display, final String gid, final String contract_id, final ShowContractDialog scd, final String rates) {
		final Shell dialog = new Shell(display, SWT.CLOSE);
		dialog.setLayout(new RowLayout(SWT.VERTICAL));
		
		Composite singlepayment = new Composite(dialog,SWT.NONE);
		singlepayment.setLayout(new RowLayout(SWT.HORIZONTAL));
		Label l_singlepayment = new Label(singlepayment, SWT.NONE);
		l_singlepayment.setText("Summe:");
		l_singlepayment.setLayoutData(new RowData(100,25));
		final Text t_singlepayment = new Text(singlepayment,SWT.NONE);
		t_singlepayment.setLayoutData(new RowData(100,20));
		
		Composite book_singlepayment = new Composite(dialog,SWT.NONE);
		book_singlepayment.setLayout(new RowLayout(SWT.HORIZONTAL));
		Label l_book_singlepayment = new Label(book_singlepayment, SWT.NONE);
		l_book_singlepayment.setText("Eingezahlt am:");
		l_book_singlepayment.setLayoutData(new RowData(100,25));
		final Text t_book_singlepayment = new Text(book_singlepayment,SWT.NONE);
		t_book_singlepayment.setLayoutData(new RowData(100,20));
	
		final Button adjust_runtime = new Button(dialog, SWT.RADIO);
		adjust_runtime.setText("Laufzeit anpassen");		
		adjust_runtime.setSelection(true);
		
		final Button adjust_rates = new Button(dialog, SWT.RADIO);
		adjust_rates.setText("Raten anpassen");		
		
		final Button adjust_goal = new Button(dialog, SWT.RADIO);
		adjust_goal.setText("Zielsumme anpassen");		
		
	    Composite bookbutton = new Composite(dialog,SWT.RIGHT_TO_LEFT); 
	    bookbutton.setLayout(new RowLayout(SWT.VERTICAL));
	    bookbutton.setLayoutData(new RowData(250,30));
		Button book = new Button(bookbutton,SWT.PUSH);
		book.setText("Einmalzahlung erstellen");
		book.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				int adjust = 0;
				if(adjust_runtime.getSelection()==true)
					adjust = 1;
				else if(adjust_rates.getSelection()==true)
					adjust = 2;
				else if(adjust_goal.getSelection()==true)
					adjust = 3;
				MySQLConnection.createnewsinglepayment(t_singlepayment.getText(), t_book_singlepayment.getText(), gid, contract_id,rates, adjust);
				dialog.close();
				scd.update(display, shell, gid, contract_id);
			}
		});				
	    dialog.pack();
	    
	    Rectangle shellBounds = shell.getBounds();
	    Point dialogSize = dialog.getSize();
	    dialog.setLocation(
	      shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
	      shellBounds.y + (shellBounds.height - dialogSize.y) / 2);
		dialog.open();	
	}
}
